<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.Category"%>
<%@page import="entities.User"%>
<%@page import="java.util.Map"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="entities.Article"%>
<%@page import="java.util.List"%>
<%
User author = (User) request.getAttribute("author");
List<Article> articles = (List<Article>) request.getAttribute("articles"); 
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
Category category = null;

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();

//Paginator
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");

//Set content for seo
String nameServer = UrlHelper.getDomainServer(request);
String title = author.getName() + " (" + nameServer + " Author) ";
String description = "Author " + author.getName() + " joined on " + author.getCreated() + ". " + author.getAboutme();
String keywords = author.getName() + ", author info, archives, archives article, submit article, article commit";
String url = UrlHelper.getServerUrl(request) + "/u/" + author.getId();
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>

            <div id="mainContent" class="left" itemscope itemtype="http://schema.org/ItemList">
            	<div id="authorInfoPage">
	                <h2 class="title" itemprop="name"><%=author.getName() %> info</h2>
	                <meta itemprop="mainContentOfPage" content="true"/>
					<meta itemprop="itemListOrder" content="Descending" />
					<div class="authorInfo clearafter" itemscope itemtype="http://schema.org/Person">
	                	<div class="left">
	                		<% 
								if (author.getAvatar() != null && !author.getAvatar().isEmpty())  {
									 BlobKey blobKey = new BlobKey(author.getAvatar());
							%>
							<img alt="" src="<%=imagesService.getServingUrl(blobKey) %>=s100-c">
							<% } else { %>							
							<img itemprop="image" class="img-full-width" src="/images/icon-author.jpg" />
							<% } %>
	                    </div>
	                    <div class="right">
	                    	<h2 itemprop="name" class="authorName"><%=author.getName() %></h2>
	                    	<% if (!author.getWebsite().isEmpty()) { %>
	                        <p>Website : <a href="<%=author.getWebsite() %>" target="_blank" itemprop="url"><%=UrlHelper.fomatDomains(author.getWebsite()) %></a></p>
	                        <% } %>
	                        <script type="text/javascript"><!--
							google_ad_client = "ca-pub-2883566427648799";
							/* question 468x60 */
							google_ad_slot = "8574497481";
							google_ad_width = 468;
							google_ad_height = 60;
							//-->
							</script>
							<script type="text/javascript"
							src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
							</script>
	                    </div>
	                    <div class="clearbreak"></div>
	                    <p class="intro" itemprop="description"><%=author.getAboutme() %></p>                        
	                </div>
					<div class="authorArticle">
						<% 
		                for (int i = 0; i < articles.size(); i++) {
		                	category = categories.get(articles.get(i).getCategory());
		                %>
		                <article class="articleItem" itemscope itemtype="http://schema.org/Article" itemprop="itemListElement">
		                    <h2 class="articleTitle" itemprop="name">
		                    	<a href="/a/<%=articles.get(i).getAlias() %>"><%=articles.get(i).getTitle() %></a>
		                    </h2>
		                    <div class="articleMeta clearafter">
		                        by <a href="/u/<%=author.getId()%>" itemprop="author"><%=author.getName() %></a>  
		                        | on <span itemprop="dateCreated"><%=DateHelper.getStringFromDate(articles.get(i).getCreated(), "MMM dd, yyyy") %></span>
		                        <a href="/c/<%=category.getAlias() %>" class="articleCat right"><%=category.getName() %></a>
		                    </div>
		                    <div class="entry clearafter" itemprop="description">
		                    	<% if (i < 3) { %>
			                    <div class="adv">
									<script type="text/javascript"><!--
									google_ad_client = "ca-pub-2883566427648799";
									/* link_menu 468x15 */
									google_ad_slot = "8735411486";
									google_ad_width = 468;
									google_ad_height = 15;
									//-->
									</script>
									<script type="text/javascript"
									src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
									</script>
				                </div>
				                <% } %>
		                        <p><%=StringHelper.cutString(StringHelper.html2text(articles.get(i).getContent()), 40) %>...</p>
		                    </div>
		                    <a href="/a/<%=articles.get(i).getAlias() %>" class="readMore">read more</a>
		                </article>
		                <% } %>
	                </div>
	                <ul class="pagination">
	                    <%
						List<Integer> range = paginator.getRangePage();
						%>
	                    <% if (prevPage) { %>
						<li><a href="<%=paginator.getPrev()%>">Prev</a></li>
						<% } %>
						<%
						if (nextPage || prevPage) {
							for (int i = 0; i < range.size(); i++) {
						%>
						<li><a class="<%=range.get(i).intValue() == pages ? "current" : "" %>" href="<%=range.get(i).intValue() == pages ? "javascript: return false;" : paginator.getPage(range.get(i).intValue())%>"> <%=range.get(i).toString() %> </a></li>
						<%  }
						} %>
						
						<% if (nextPage) { %>
						<li><a href="<%=paginator.getNext()%>"> Next </a></li>
						<% } %>
	                </ul>
	            </div>
            </div>
            
            <jsp:include page="/elements/right.jsp" />
            
            <div class="clearbreak"></div>
<jsp:include page="/layouts/footer.jsp" />